package vn.fpt.fsoft.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

import vn.fpt.fsoft.entity.County;
import vn.fpt.fsoft.util.HibernateUtil;

/*
 * Class GovOfficeRegionDAO
 *
 * Version 1.0
 *
 * 22-August-2014
 *
 * Modification Logs:
 * DATE                 AUTHOR              DESCRIPTION
 * --------------------------------------------------------
 * 22-August-2014       NguyenTuyen          Create GovRegionDAO
 */
public class CountyDAO {
    
    /**
     * Session.
     */
    private Session session;
    
    /**
     * Constructor.
     */
    public CountyDAO() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }
    
    /**
     * Get all county in country.
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<County> getAllCounty() {
        List<County> ds = null;
        try {
            session.beginTransaction();
            Criteria cri = session.createCriteria(County.class).addOrder(
                    Order.asc("countyName"));
            ds = cri.list();
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        } finally {
            session.close();
        }
        return ds;
    }
    
    /**
     * Get a county.
     * 
     * @param countyID
     * @return
     */
    public County getCountyByID(int countyID) {
        County c = null;
        try {
            session.beginTransaction();
            c = (County) session.get(County.class, countyID);
            session.getTransaction().commit();
            return c;
        } catch (HibernateException e) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return c;
    }
}
